<?php
if(!empty($_POST['submit']))
{
	$_POST['nums']=empty($_POST['nums'])?1:$_POST['nums'];
	$_POST['catid']=empty($_POST['catid'])?0:$_POST['catid'];
	$_POST['statu']=empty($_POST['statu'])?0:$_POST['statu'];
	$click_nums=$_POST['click_nums']*1;
	
	$catid = $_POST['sub_catid']>0?$_POST['sub_catid']:$_POST['catid'];
	
	include_once($config['webroot'].'/lib/allchar.php');
	$str=c(trim($_POST['name']));
	$fstr=substr($str,0,1);
	$time=time();

	if($_POST['addtype']==2)
	{
		foreach(explode("\r\n",$_POST['name']) as $na)
		{
			$str=c(trim($na));
			$fstr=substr($str,0,1);
			$sql="insert into ".BRAND." (name,catid,time,char_index,char_all)
			 values ('$na','$catid','$time','$fstr','$str')";
			$db->query($sql);
		}
	}
	else
	{
		if($_POST['country']=='china'||$_POST['country']=='中国')
		{
			$_POST['province']=$_POST['provinces'];
			$_POST['city']=$_POST['citys'];
		}
		if(!empty($_GET['id']))
		{
			$sql="update ".BRAND." set name='".$_POST['name']."',catid='$catid',nums='".$_POST['nums']."',$ssql
			con='".$_POST['con']."',company='".$_POST['company']."',tel='".$_POST['tel']."',
			pic='".$_POST['pic']."',statu='".$_POST['statu']."',url='$_POST[url]',country='$_POST[country]',province='$_POST[province]',
			city='$_POST[city]',time='$time',inner_url='$_POST[inner_url]',char_index='$fstr',char_all='$str',click_nums='$click_nums'
			where id='".$_GET['id']."'";//update
		}
		else
		{
			$sql="insert into ".BRAND." 
			(name,catid,con,pic,nums,company,tel,statu,url,country,province,city,time,inner_url,char_index,char_all,click_nums) values 
			('$_POST[name]','$catid','".$_POST['con']."','".$_POST['pic']."','".$_POST['nums']."','".$_POST['company']."',
			'".$_POST['tel']."','".$_POST['statu']."','$_POST[url]','$_POST[country]','$_POST[province]','$_POST[city]','$time','$_POST[inner_url]','$fstr','$str','$click_nums')";//new insert
		}
		$db->query($sql);
	}
	admin_msg('module.php?m=brand&s=brand_list.php','操作成功');
}
$country=country_list();
//-------------------------------------
$db->query( "select * from ".BRANDCAT.' order by sort');
while($pl=$db->fetchRow())
{
	if($pl['pid']>0)
	{
		$cat['list']["$pl[pid]"]['sub'][]=array('id'=>$pl['id'],'cat'=>$pl['catname']);
		if($de&&$de['catid']==$pl['id'])
			$cat['sid']  = $pl['pid'];
	}
	else
	{
		$cat['sid'] = $cat['sid']>0?$cat['sid']:0;
		$cat['list']["$pl[id]"]['cat'] = $pl['catname'];
	}
}
//-------------------------------------
if(!empty($_GET['id']))
{
	$sql="select * from ".BRAND." where id='$_GET[id]'";
	$db->query($sql);
	$de=$db->fetchRow();
}
//-------------------------------------
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE><?php echo lang_show('admin_system');?></TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="main.js"></script>
<script>
closeimg='<?php echo $config['weburl'];?>/image/default/icon_close.gif';
weburl='<?php echo $config['weburl'];?>';
</script>
<script src="../script/my_lightbox.js" language="javascript"></script>
<script type="text/javascript" src="../script/prototype.js"></script>
<script>
function _hideSubCat(){
	var sub_cat  = $('sub_cat').getElementsByTagName('select');
	if(sub_cat.length>0)
		for(var i=0;i<sub_cat.length;i++){
			sub_cat[i].style.display='none';
			sub_cat[i].style.disabled = true;
		}
}
</script>
</HEAD>
<body>

<div class="bigbox">
	<div class="bigboxhead"><?php echo lang_show('brand');?></div>
    <div class="bigboxbody">
 <form action="" method="post" enctype="multipart/form-data">
   <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="left">&nbsp;</td>
    <td>
      <input onclick="window.location='module.php?m=brand&s=add_brand.php&addtype=1';" name="addtype" type="radio" value="1" checked="checked" />
      单个发布　
      <input <?php if($_GET['addtype']==2) echo 'checked="checked"';?> onclick="window.location='module.php?m=brand&s=add_brand.php&addtype=2';" type="radio" name="addtype" value="2" />
    批量发布</td>
  </tr>
  <tr>
    <td class="body_left"align="left"><?php echo lang_show('name');?></td>
    <td width="85%">
	<?php if($_GET['addtype']==2){?>
      <textarea name="name" class="text" rows="20"></textarea>
      <br />
	 每行一个
	 <?php }else{ ?><input name="name" type="text" value="<?php echo $de['name'];?>" class="text"  > 
	 <?php } ?>	</td>
    </tr>
	  <tr>
    <td  align="left">品牌类别</td>
    <td >
      <select class="select" name='catid' onchange="_hideSubCat();if(this.value>0){$('cat_'+this.value).style.display='';$('cat_'+this.value).disabled=false;}">
			<option value=''>选择品牌类别</option>
			<?php
			if(is_array($cat['list']))
			{
				foreach($cat['list'] as $k=>$pl){
					echo "<option";
					if($de['catid']==$k)
						echo " selected ";
					echo " value='$k'>$pl[cat]</option>";
				}
			}
			?>
	  </select>
	  <span id='sub_cat'>
				<?php
				if(is_array($cat['list']))
				{
					foreach($cat['list'] as $k=>$pl){
						if($pl['sub'])
						{
							echo "<select id='cat_".$k."' name='sub_catid' ";
							if($cat['sid']==$k)
								echo " style='display:inline_block' ";
							else{
								echo " style='display:none' disabled='disabled' ";
							}
							echo "'>";
							echo "<option value=''>选择品牌类别</option>";
							foreach($pl['sub'] as $sub){
								echo "<option";
								if($de['catid']==$sub['id'])
									echo " selected ";
								echo " value='$sub[id]'>$sub[cat]</option>";
							}
							echo "</select>";
						}
					}
				}
			?>	
	  </span>    </td>
    </tr>
<?php if($_GET['addtype']!=2){ ?>
  <tr>
    <td align="left"><?php echo lang_show('company');?></td>
    <td>
      <input name="company" type="text" value="<?php echo $de['company'];?>" class="text" >    </td>
    </tr>
 
  <tr>
    <td align="left"><?php echo lang_show('tel');?></td>
    <td>
      <input name="tel" type="text" value="<?php echo $de['tel'];?>" class="text" >    </td>
    </tr>
  <tr>
    <td align="left">官方网址</td>
    <td>
      <input name="url" type="text" id="url" value="<?php if(empty($de['url'])) echo 'http://'; else echo $de['url'];?>" class="text">    </td>
    </tr>
	  <tr>
    <td align="left">站内站台</td>
    <td>
      <input name="inner_url" type="text" id="inner_url" value="<?php if(empty($de['inner_url'])) echo 'http://'; else echo $de['inner_url'];?>" class="text">    </td>
    </tr>
  <tr>
    <td align="left">所在国家</td>
    <td>
	<script>
	function s_pc(v)
	{
		if(v=='中国')
		{
			$('sc').style.display='block';
			$('wc').style.display='none';
		}
		else
		{
			$('sc').style.display='none';
			$('wc').style.display='block';
		}
	}
	</script>
      <select class="select" onchange="s_pc(this.value);" name="country">
	  <?php foreach($country as $v){?>
	  <option <?php if($de['country']==$v['name']) echo 'selected="selected"';?>><?php echo $v['name'];?></option>
	  <?php } ?>
      </select>     </td>
    </tr>
  <tr>
    <td align="left">省份城市</td>
    <td>
	<style>
	<?php
	if($de['country']=='中国'||$de['country']=='china'||empty($de['country']))
	{
		echo "#sc{display:block;}";
		echo "#wc{display:none;}";
	}
	else
	{
		echo "#sc{display:none;}";
		echo "#wc{display:block;}";
	}
	?>
	</style>
	  <div id="sc">
		  <?php 
		  $de['citys']=$de['city'];
		  include($config['webroot'].'/admin/include_prv_city.php');
		  ?>
	  </div>
	  <div id="wc">
		  <input class="ltext" name="province" type="text" id="province" value="<?php echo $de['province'];?>" />--
		  <input class="rtext" name="city" type="text" id="city" value="<?php echo $de['city'];?>" />
	  </div>	  </td>
  </tr>
  <tr>
    <td align="left"><?php echo lang_show('brand_des');?></td>
    <td>
		<?php
			$BasePath = "../lib/fckeditor/";
			include($BasePath."fckeditor.php");	
			$fck_en = new FCKeditor('con') ;
			if($config['language']=='cn')
				$fck_en->Config['DefaultLanguage']='zh-cn';
			else
				$fck_en->Config['DefaultLanguage']='en';
			$fck_en->BasePath    = $BasePath ;
			$fck_en->ToolbarSet  = 'Default' ;
			$fck_en->Width='100%';
			$fck_en->Height=500;
			$fck_en->Config['ToolbarStartExpanded'] = true;
			$fck_en->Value = stripslashes($de['con']);
			echo $fck_en->CreateHtml();
		?>	</td>
  </tr>
  <tr>
    <td align="left">LOGO</td>
    <td><input name="pic" type="text" id="pic" value="<?php echo $de['pic'];?>" class="text">
		 [<a href="javascript:uploadfile('上传LOGO','pic',200,150)">上传</a>] 
		 [<a href="javascript:preview('pic');">预览</a>]
		 [<a onclick="javascript:$('pic').value='';" href="#">删除</a>]	</td>
  </tr>
  <tr>
    <td align="left"><?php echo lang_show('nums');?></td>
    <td><input value="<?php echo $de['nums'];?>" name="nums" type="text" maxlength="4" class="text"></td>
    </tr>
  <tr>
    <td align="left">点击数</td>
    <td><input value="<?php echo $de['click_nums'];?>" name="click_nums" type="text" maxlength="8" class="text"></td>
  </tr>
  <tr>
    <td align="left">状态</td>
    <td>
	<select class="select" name="statu">
      <option <?php if(empty($de['statu'])) echo 'selected="selected"';?> value="0">隐藏</option>
      <option <?php if($de['statu']==1) echo 'selected="selected"';?> value="1">显示</option>
      <option <?php if($de['statu']==2) echo 'selected="selected"';?> value="2">推荐</option>
    </select></td>
    </tr>
	<?php } ?>
  <tr>
    <td align="left">&nbsp;</td>
    <td><input class="btn" type="submit" name="submit" id="button" value="<?php echo lang_show('submit');?>"></td>
    </tr>
</table>
</form>
</div>
</div>
</body>
</html>